﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Epitech.Krisk.Xna.ScreenManagerLib.ScreenManager;
using Epitech.Krisk.Xna.UI.Game.InGame.Gameplay;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Epitech.Krisk.Xna.UI.Game.InGame
{
    public class ToolTip : AGameObject
    {
        private Texture2D _blank;
        private Texture2D _soldier;
        private Texture2D _tank;
        private Texture2D _warplane;
        private Vector2 _pos;

        public Territory Territory { get; set; }
        public SpriteFont Font { get; set; }

        public override void LoadContent()
        {
            _blank = Content.Load<Texture2D>("blank");
            _warplane = Content.Load<Texture2D>("Game/Avion_Sprite");
            _tank = Content.Load<Texture2D>("Game/Char_Sprite");
            _soldier = Content.Load<Texture2D>("Game/Soldat_Sprite");
            Font = Content.Load<SpriteFont>("Font/defaultfont");
        }

        public override void HandleInput(InputState input)
        {
            _pos = input.MousePosition();
        }

        public override void Draw(GameTime gameTime)
        {
            if (_pos.Y <= 800)
            {
                int xp = _pos.X + 230 > ViewPort.Width ? -210 : 25;
                int yp = _pos.Y + 260 > 800 ? -231 : 0;
                Rectangle tooltip = new Rectangle((int) _pos.X + xp, (int) _pos.Y + yp, 200, 250);

                Rectangle soldierrec = new Rectangle(tooltip.X + 24, tooltip.Y + tooltip.Height - 130, 12, 32);
                Rectangle tankrec = new Rectangle(tooltip.X + 5, tooltip.Y + tooltip.Height - 90, 51, 32);
                Rectangle warplanerec = new Rectangle(tooltip.X + 8, tooltip.Y + tooltip.Height - 50, 45, 32);

                SpriteBatch.Begin();
                SpriteBatch.Draw(_blank, tooltip, new Color(0, 0, 0, 128));
                if (Territory != null && Font != null)
                {
                    float scale = 0.5f;

                    while (Font.MeasureString(Territory.Name).X*scale > tooltip.Width)
                        scale -= 0.05f;

                    Vector2 pos = new Vector2(
                        tooltip.X + tooltip.Width / 2 - Font.MeasureString(Territory.Name).X * scale / 2, tooltip.Y + 10);
                    SpriteBatch.DrawString(Font, Territory.Name, pos, Color.White, 0f, Vector2.Zero, scale,
                                           SpriteEffects.None, 0f);
                    if (Territory.Owner != null)
                    {
                        scale = 0.5f;
                        while (Font.MeasureString(Territory.Owner.Name).X*scale > tooltip.Width)
                            scale -= 0.05f;
                        pos =
                            new Vector2(
                                tooltip.X + tooltip.Width/2 - Font.MeasureString(Territory.Owner.Name).X*scale/2,
                                tooltip.Y + 50);
                        SpriteBatch.DrawString(Font, Territory.Owner.Name, pos, Color.White, 0f, Vector2.Zero, scale,
                                               SpriteEffects.None, 0f);
                        pos = new Vector2(soldierrec.X + 120, soldierrec.Y);
                        SpriteBatch.DrawString(Font, Territory.Owner.OwnTerritory[Territory].Soldier.ToString(), pos,
                                               Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
                        pos = new Vector2(soldierrec.X + 120, tankrec.Y);
                        SpriteBatch.DrawString(Font, Territory.Owner.OwnTerritory[Territory].Tank.ToString(), pos,
                                               Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
                        pos = new Vector2(soldierrec.X + 120, warplanerec.Y);
                        SpriteBatch.DrawString(Font, Territory.Owner.OwnTerritory[Territory].WarPlane.ToString(), pos,
                                               Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
                    }
                    // TODO : Draw player name (add in Territory class a ref of the owner of the Territory)
                }
                SpriteBatch.Draw(_soldier, soldierrec, Color.White);
                SpriteBatch.Draw(_tank, tankrec, Color.White);
                SpriteBatch.Draw(_warplane, warplanerec, Color.White);
                SpriteBatch.End();
            }
        }
    }
}
